function amguiscan
%AMGUI Summary of this function goes here
%   Detailed explanation goes here
global amrack amscan amaux;

hdata.fighand=figure(235);
set(hdata.fighand,'Name','Automeasure Scan','NumberTitle','off','MenuBar','none','Toolbar','none')
panelprops={'BackgroundColor','white'};
inputprops={'units','normalized','HorizontalAlignment','left'};

hdata.leftpanel=uipanel('Title','Stuff','Position',[0,0,1/3,1]);
hdata.rightpanel=uipanel('Title','Loops','Position',[1/3,0,2/3,1]);
hdata.filename=uicontrol('Parent',hdata.leftpanel,inputprops{:},'Style','edit','Position',[0.1,0.9,0.8,0.05]);
hdata.gobutton=uicontrol('Parent',hdata.leftpanel,'units','normalized','Style','pushbutton','Position',[0,0,1,0.2],'String','START','backgroundcolor','green','FontSize',20);

guidata(hdata.fighand,hdata);
refreshloops(hdata.fighand);


end


%% display loops
function refreshloops(hObject)
global amscan amrack;
hdata=guidata(hObject);
delete(get(hdata.rightpanel,'Children'));
if exist('hdata.loops','var'),clear(hdata.loops);end
for i=1:length(amscan.loops)
    hdata.loops(i).panel=uipanel('Parent',hdata.rightpanel,'Title',num2str(i),'Position',[0,1-1/3*i,1,1/3]);
    hdata.loops(i).npts=uicontrol('Parent',hdata.loops(i).panel,'Units','normalized','style','edit','Position',[0,0.9,0.1,0.1],'String',num2str(amscan.loops(i).npoints));
    hdata.loops(i).pointrate=uicontrol('Parent',hdata.loops(i).panel,'Units','normalized','style','edit','Position',[0.1,0.9,0.1,0.1],'String',num2str(amscan.loops(i).pointrate));
    hdata.loops(i).addsc=uicontrol('Parent',hdata.loops(i).panel,'Units','normalized','Style','pushbutton','BackgroundColor','Green','Position',[0.55,0.9,0.05,0.1],'String','+','Callback',{@addsc,i});
    hdata.loops(i).scpanel=uipanel('Parent',hdata.loops(i).panel,'Position',[0,0,1,0.9],'BorderWidth',0);
    guidata(hdata.fighand,hdata);
    refreshsetchans(hdata.fighand,i);
    refreshgetchans(hdata.fighand,i);
    refreshplots(hdata.fighand,i);
end
end
%% display set chans
function refreshsetchans(hObject,i)
global amscan amrack;
hdata=guidata(hObject);
thisloop=amscan.loops(i);
valrates=thisloop.valrates;
delete(get(hdata.loops(i).scpanel,'Children'));
clear('hdata.loops.sc');

%delete(allchild(hdata.loops(i).scpanel));
for j=1:length(thisloop.setchans)
    thissc=thisloop.setchans(j);
    hdata.loops(i).sc(j).panel=uipanel('Parent',hdata.loops(i).scpanel,'Position',[0,1-j/3,0.6,1/3]);
    setchanprops={'Parent',hdata.loops(i).sc(j).panel,'Units','normalized'};
    hdata.loops(i).sc(j).channel=uicontrol(setchanprops{:},'Style','Popupmenu','Position',[0,0,0.3,1],'String',{amrack.rackchans.name},'Value',find(thissc==amrack.rackchans),'Callback',{@editsetchan,i,j,'channel'});
    hdata.loops(i).sc(j).start=uicontrol(setchanprops{:},'Style','Edit','Position',[0.3,0,0.1,1],'String',num2str(thisloop.startpts(j)),'Callback',{@editsetchan,i,j,'startpts'});
    hdata.loops(i).sc(j).end=uicontrol(setchanprops{:},'Style','Edit','Position',[0.4,0,0.1,1],'String',num2str(thisloop.endpts(j)),'Callback',{@editsetchan,i,j,'endpts'});
    hdata.loops(i).sc(j).stepsize=uicontrol(setchanprops{:},'Style','Edit','Position',[0.5,0,0.1,1],'String',num2str(abs(thisloop.startpts(j)-thisloop.endpts(j))/thisloop.npoints),'Callback',{@editsetchan,i,j,'stepsize'});
    hdata.loops(i).sc(j).chanrate=uicontrol(setchanprops{:},'Style','Edit','Position',[0.6,0,0.1,1],'String',num2str(valrates(j)),'Callback',{@editsetchan,i,j,'chanrate'});
    hdata.loops(i).sc(j).del=uicontrol(setchanprops{:},'Style','pushbutton','BackgroundColor','Red','Position',[0.95,0,0.05,1],'String','X','Callback',{@removesc,i,j});
end
guidata(hdata.fighand,hdata);
end
%% display get chans
function refreshgetchans(hObject,loopnum)
global amscan;
hdata=guidata(hObject);
hdata.loops(loopnum).getchans=uicontrol('Parent',hdata.loops(loopnum).panel,'Units','normalized','Style','Listbox','Position',[0.6,0.1,0.1,0.8],'String',{amscan.loops(loopnum).getchans.name});
hdata.loops(loopnum).getbutton=uicontrol('Parent',hdata.loops(loopnum).panel,'Units','normalized','Style','pushbutton','Position',[0.6,0,0.1,0.1],'String','Edit','Callback',{@editgetchans,loopnum});
guidata(hdata.fighand,hdata);
end
%% display plots
function refreshplots(hObject,loopnum)
global amscan;
hdata=guidata(hObject);
thisloop=amscan.loops(loopnum);
for j=1:length(thisloop.plots)
    thisplot=thisloop.plots(j);
    hdata.loops(loopnum).plot(j).panel=uipanel('Parent',hdata.loops(loopnum).panel,'Position',[0.7,0.9-0.3*j,0.3,0.3]);
    loopprops={'Parent',hdata.loops(loopnum).plot(j).panel,'Units','normalized'};
    plottypes={'XY','XYY','XYZ'}; %do an if here so that loop 1 can't have XYZ
    hdata.loops(loopnum).plot(j).type=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0,0,0.25,1],'String',plottypes,'Value',find(strcmp(thisplot.plottype,plottypes)));
    switch thisplot.plottype
        case 'XY'
            hdata.loops(loopnum).plot(j).xchan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.25,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.xchan));
            hdata.loops(loopnum).plot(j).ychan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.5,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.ychan));
        case 'XYY'
            hdata.loops(loopnum).plot(j).xchan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.25,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.xchan));
            hdata.loops(loopnum).plot(j).ychan1=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.5,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.ychan(1,:)));
            hdata.loops(loopnum).plot(j).ychan2=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.75,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.ychan(2,:)));
        case 'XYZ'
            hdata.loops(loopnum).plot(j).xchan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.25,0,0.25,1],'String',{amscan.loops(loopnum-1).allchans.name},'Value',find(amscan.loops(loopnum-1).allchans==thisplot.xchan));
            hdata.loops(loopnum).plot(j).ychan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.5,0,0.25,1],'String',{thisloop.allchans.name},'Value',find(thisloop.allchans==thisplot.ychan));
            hdata.loops(loopnum).plot(j).zchan=uicontrol(loopprops{:},'Style','Popupmenu','Position',[0.75,0,0.25,1],'String',{amscan.loops(loopnum-1).allchans.name},'Value',find(amscan.loops(loopnum-1).allchans==thisplot.zchan(2,:)));
    end
end
guidata(hObject,hdata);
end
%% edit set channels callback
function editsetchan(hObject,~,i,j,field)
global amscan amrack;
switch field
    case 'channel'
        amscan.loops(i).setchans(j)=amrack.rackchans(get(hObject,'Value'));
    case {'startpts','endpts'}
        val=get(hObject,'String');
        if isnumeric(str2double(val))
            amscan.loops(i).(field)(j)=get(hObject,'Value');
        else
            errordlg('Input must be a number','Error');
            set(hObject,'String',num2str(amscan.loops(i).(field)(j)));
        end        
    case 'stepsize' %edit number of points in the loop
        val=get(hObject,'String');
        if isnumeric(str2double(val)) && str2double(val)>0
            amscan.loops(i).setstepsize(j,val);
        else
            errordlg('Input must be a positive number','Error');
        end
        refreshsetchans(hObject,i) %do refresh because points of other set chans will change
    case 'chanrate'
        val=get(hObject,'String');
        if isnumeric(str2double(val)) && str2double(val)>0
            amscan.loops(i).setvalrate(j,val);
        else
            errordlg('Input must be a positive number','Error');
        end
        refreshsetchans(hObject,i) %do refresh because rates of other set chans will change
end
end
%% edit get channels callback
function editgetchans(hObject,~,i)
global amscan amrack;
%hdata=guidata(hObject);
initvals=cellfun(@(x) find(x==amrack.rackchans),num2cell(amscan.loops(i).getchans));
[selection,ok]=listdlg('ListString',{amrack.rackchans.name},'PromptString','Select channels to record','InitialValue',initvals);
if ok
    amscan.loops(i).getchans=amrack.rackchans(selection);
    refreshloops(hObject);
end
end
%% delete set channel
function removesc(hObject,~,loopnum,scnum)
global amscan;
hdata=guidata(hObject);
amscan.loops(loopnum).rmsetchan(scnum);
refreshloops(hObject);
end
function addsc(hObject,~,loopnum)
global amscan;
amscan.loops(loopnum).addsetchan;
refreshloops(hObject);
end
